//
//  LoadDetailsCoupons.m
//  Truffle
//
//  Created by raca on 16/03/10.
//  Copyright 2010 isrtream1-svn . All rights reserved.
//

#import "LoadDetailsCoupon.h"
#import "RestConnection.h"
#import "DatasTruffle.h"
#import "DAOTruffle.h"
#import "Gateway.h"
#import "Coupon.h"


@implementation LoadDetailsCoupon

@synthesize  myCaller, myRestConnection, idCurrentCoupon, providerCurrentCoupon;






#pragma mark RestConnectionDelegate

- (void)willSendRequest:(NSURLRequest *)request
{
	NSLog(@"****************** [LoadDetailsCoupon] [willSendRequest] CouponDetails : will send request : %@",request);
}


- (void)didReceiveResponse:(NSURLResponse *)response
{
	NSLog(@"****************** [LoadDetailsCoupon] [didReceiveResponse]: %@", response);
}


- (void)finishedReceivingData:(NSData *)data callback:(NSString *)restconn
{
	
	NSLog(@"****************** [LoadDetailsCoupon] [finishedReceivingData] CouponDetails :  %@", [myRestConnection stringData]);
	
	// Get the dictionary containing the raw parsed data.
	NSDictionary *rawDetailsData = [myRestConnection dictionaryData];
	// récupérer les informations du coupon en cours, et les mettre dans un tableau
	couponInformationsArray = [[rawDetailsData objectForKey:@"informations"] retain];
	NSString * couponDescription = @"";
	
	// récupérer les label/value du champs inforamations
	for (int cpt=0; cpt<[couponInformationsArray count]; cpt++){
		// Extract info from JSon string
		couponDescription = [couponDescription stringByAppendingFormat:@"%@ : %@\n",
							 [[couponInformationsArray objectAtIndex:cpt] objectForKey:@"label"], 
							 [[couponInformationsArray objectAtIndex:cpt] objectForKey:@"value"]];
	}
	
	// TODO : changer les valeurs des champs : id et pas name ....
	Coupon * currentCoupon = [[Coupon alloc]initWithId:[self idCurrentCoupon]
												  name:[rawDetailsData objectForKey:@"name"] 
											  position:[rawDetailsData objectForKey:@"position"] 
											  provider:[self providerCurrentCoupon] 
											couponCode:[rawDetailsData objectForKey:@"code"] 
										  descriptions:couponDescription
							  ];
	[[DatasTruffle getDao] addCoupon:currentCoupon];
	[currentCoupon release];
	[myCaller refreshList];
}




@end
